package com.back.web;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

import com.back.business.IUserTableService;
import com.back.business.UserTableServiceImpl;
import com.base.web.BaseAction;
import com.domain.UserTable;
import com.util.comm.RequestUtil;

public class UserTableAction extends BaseAction {

	private IUserTableService userTableService;
	private static final Logger LOGGER = Logger
			.getLogger(UserTableAction.class);

	public UserTableAction() {
		userTableService = new UserTableServiceImpl();
	}

	/**
	 * message 为true登陆成功，为false登入失败
	 * 
	 * @return
	 */
	public String login() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			UserTable userTable = new UserTable();
			String userName = RequestUtil.getParam(request, "userName", "");
			String password = RequestUtil.getParam(request, "password", "");
			userTable.setLoginName(userName);
			userTable.setPassword(password);
			boolean loginResult = userTableService.login(userTable);
			String message = "false";
			if (loginResult) {
				message = "true";
				RequestUtil.setLoginUser2Session(request, "login", userTable);
			}
			out.println("{result:" + message + "}");
		} catch (IOException e) {
			e.printStackTrace();
			LOGGER.error("userTableAction  login error", e);
		}

		return null;
	}
	
public String  modifyPwById (){
		
		String  message ="false";
		UserTable  userTabl = (UserTable) RequestUtil.getLoginUserFromSession(request,"login");
		//String oldPw =  RequestUtil.getParam(request, "old", "");
		String newPw =  RequestUtil.getParam(request, "newPw", "");
		Long id =Long.parseLong("1") ;//userTabl.getId();
		
		  if(userTableService.modifyPwById(id,newPw)){
			  message = "true";
		  }else{
			  message ="false";
		  }
		return null;
	}
	
	public String findPw(){
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String message = "false";
			UserTable  userTabl = (UserTable) RequestUtil.getLoginUserFromSession(request,"login");
			//if(userTabl!= null){
			String passWord = "123";//userTabl.getPassword();
			String oldPw =  RequestUtil.getParam(request, "password", "");
			String newPw =  RequestUtil.getParam(request, "newPw", "");
			if(passWord.equals(oldPw)){
				modifyPwById();
				message = "true";
			}
			out.println("{result:"+message+"}");
			//}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
   public  String   exit(){
	   RequestUtil.setLoginUser2Session(request, "login", null);
	   return "exit";
   }
}
